<template>
  <div class="handle" >

  </div>
</template>

<script>
import {getUrlParam} from '@/assets/js/common.js'
export default {
  data () {
    return {
      code: '', // 登录验证
      state: '',
      t: 0,
      id: 0
    }
  },
  mounted () {
    sessionStorage.removeItem('token')
    this.code = getUrlParam('code')
    this.state = getUrlParam('state')
    this.t = getUrlParam('t') || 0
    this.id = getUrlParam('id') || 0
    console.log(this.code, this.state)
    this.getToken()
    // this.login()
  },
  methods: {
    login () {
      let urlParams = {
        id: 28
      }
      this.$api.login(urlParams).then(res => {
        sessionStorage.setItem('token', res.params.token)
        this.getWx()
        let path = '/index'
        switch (Number(this.t)) {
          case 1:path = '/subscribe'; break
          case 2:path = '/detail/' + this.id; break
          default:path = '/index'
        }
        this.$router.replace({
          path: path
        })
      }).catch(error => {
        console.log(error)
      })
    },
    getToken () {
      let body = {
        code: this.code,
        state: this.state
      }
      this.$api.getToken(body).then(res => {
        sessionStorage.setItem('token', res.params.token)
        this.getWx()
        let path = '/index'
        switch (Number(this.t)) {
          case 1:path = '/subscribe'; break
          case 2:path = '/detail/' + this.id; break
          default:path = '/index'
        }
        this.$router.replace({
          path: path
        })
      }).catch(error => {
        console.log(error)
      })
    },
    getWx () {
      // console.log(this.$wx)
      let jsApiList = ['onMenuShareAppMessage', 'onMenuShareTimeline']    // js接口
      let curUrl = window.location.href.split('#')[0]    // 当前页面地址
      // console.log(curUrl)
      let params = {
        url: curUrl
      }
      let isDebug = false
      this.$api.getWx(params)
        .then(res => {
          console.log(res)
          if (res.code == 1) {
            this.$wechat.config({
              debug: isDebug, // 开启调试模式
              appId: res.params.appId, // 必填，公众号的唯一标识
              timestamp: res.params.timestamp, // 必填，生成签名的时间戳
              nonceStr: res.params.noncestr, // 必填，生成签名的随机串
              signature: res.params.signature, // 必填，签名
              jsApiList: jsApiList // 必填，需要使用的JS接口列表
            })
            this.$wechat.error(function (res) {
              console.log('wxjdk error')
            })
          }
        }).catch(res => {
          console.log(res)
        })
    }
  },
  watch: {

  },
  components: {

  }
}
</script>
<style scoped lang='less'>
 
</style>